package cordova_bridge;

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import android.content.Context;

import com.gonsin.nde.camera.event.CordovaBridge;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * This class echoes a string called from JavaScript.
 */
public class cordova_bridge extends CordovaPlugin {

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        if (action.equals("post")) {
            String message = args.getString(0);
            Context context = cordova.getActivity();
            String event = args.getString(0);
            Object callbackArgs = null;
            try {
                args.getString(0);
                int length = args.length();
                if (length > 1) {
                    Object[] objs = new Object[length - 1];
                    for (int i = 1; i < length; i++) {
                        objs[i - 1] = args.get(i);
                    }
                    callbackArgs = objs;
                }
            } catch (JSONException e) {
                throw new RuntimeException(e);
            }

            try {
                CordovaBridge.post(context, event, callbackArgs);
                callbackContext.success();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return true;
        }
        return false;
    }
}
